package icu.qx.wm.service.impl;

import icu.qx.common.utils.CodeUtils;
import icu.qx.wm.domain.WmStorageLocation;
import icu.qx.wm.exception.WmStorageAreaException;
import icu.qx.wm.exception.WmStorageLocationException;
import icu.qx.wm.service.IWmStorageLocationService;
import icu.qx.wm.mapper.WmStorageLocationMapper;
import icu.qx.wm.vo.WarehouseFormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * &#064;description  针对表【wm_storage_location(库位)】的数据库操作Service实现
 * &#064;createDate  2024-06-05 14:47:09
 */
@Service
public class WmStorageLocationServiceImpl implements IWmStorageLocationService {
    private final WmStorageLocationMapper wmStorageLocationMapper;
    private final CodeUtils codeUtils;

    @Autowired
    public WmStorageLocationServiceImpl(WmStorageLocationMapper wmStorageLocationMapper, CodeUtils codeUtils) {
        this.wmStorageLocationMapper = wmStorageLocationMapper;
        this.codeUtils = codeUtils;
    }

    /**
     * 查询库位
     *
     * @param slCode 库位主键
     * @return 库位
     */
    @Override
    public WmStorageLocation selectWmStorageLocationBySlCode(String slCode) {
        return wmStorageLocationMapper.selectWmStorageLocationBySlCode(slCode);
    }

    /**
     * 查询库位列表
     *
     * @param wmStorageLocation 库位
     * @return 库位
     */
    @Override
    public List<WmStorageLocation> selectWmStorageLocationList(WmStorageLocation wmStorageLocation) {
        return wmStorageLocationMapper.selectWmStorageLocationList(wmStorageLocation);
    }

    /**
     * 新增库位
     *
     * @param wmStorageLocation 库位
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = WmStorageLocationException.class)
    public int insertWmStorageLocation(WmStorageLocation wmStorageLocation) {
        try {
            compareArea(wmStorageLocation.getSlSaCode(), wmStorageLocation.getSlArea());

            wmStorageLocation.setSlCode(codeUtils.generateCode("wm_storage_location"));
            if (wmStorageLocation.getSlMaxWeight() == null) {
                wmStorageLocation.setSlMaxWeight(0.00);
            }
            if (wmStorageLocation.getSlLocationX() == null) {
                wmStorageLocation.setSlLocationX(0.00);
            }
            if (wmStorageLocation.getSlLocationY() == null) {
                wmStorageLocation.setSlLocationY(0.00);
            }
            if (wmStorageLocation.getSlLocationZ() == null) {
                wmStorageLocation.setSlLocationZ(0.00);
            }
            return wmStorageLocationMapper.insertWmStorageLocation(wmStorageLocation);
        } catch (Exception e) {
            throw new WmStorageLocationException("插入库位失败: " + e.getMessage(), e);
        }
    }

    /**
     * 修改库位
     *
     * @param wmStorageLocation 库位
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = WmStorageLocationException.class)
    public int updateWmStorageLocation(WmStorageLocation wmStorageLocation) {
        try {
            compareArea(wmStorageLocation.getSlSaCode(), wmStorageLocation.getSlArea());
            return wmStorageLocationMapper.updateWmStorageLocation(wmStorageLocation);
        } catch (Exception e) {
            throw new WmStorageLocationException("修改库位失败: " + e.getMessage(), e);
        }
    }

    private void compareArea(String slSaCode, Double slArea) {
        Double wArea = wmStorageLocationMapper.selectSaArea(slSaCode);
        if (wArea < slArea) {
            throw new WmStorageAreaException("库位面积大于库区面积");
        }
    }

    /**
     * 批量删除库位
     *
     * @param slCodes 需要删除的库位主键
     * @return 结果
     */
    @Override
    public int deleteWmStorageLocationBySlCodes(String[] slCodes) {
        return wmStorageLocationMapper.deleteWmStorageLocationBySlCodes(slCodes);
    }

    /**
     * 删除库位信息
     *
     * @param slCode 库位主键
     * @return 结果
     */
    @Override
    public int deleteWmStorageLocationBySlCode(String slCode) {
        return wmStorageLocationMapper.deleteWmStorageLocationBySlCode(slCode);
    }

    /**
     * 查询库位编码
     */
    @Override
    public  List<Map<String, String>> selectLocationCodes(WarehouseFormVo warehouseFormVo) {
        return wmStorageLocationMapper.selectLocationCodes(warehouseFormVo);
    }
}